//
//  HWVoiceRoomGiftSegmentView.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/27.
//

import UIKit
import JXSegmentedView
class HWVoiceRoomGiftSegmentView: UIView {
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    var titles = ["","",""]
    var lineView:UIView!
    var titleArr:[String] = Array()
    
    var didSelectGift:((_:HWVoiceRoomGiftModel)->())?
    
    var giftModels:[HWVoiceRoomGiftListModel]? {
        didSet{
            titles.removeAll()
            for mod in giftModels ?? [] {
                titles.append(mod.title)
            }
            segmentedDataSource.titles = titles
            segmentedView.reloadData()
        }
    }
    
    
    var pageArr:[HWVoiceRoomGiftPageView] = Array()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension HWVoiceRoomGiftSegmentView {
    private func initUI() -> Void {
        backgroundColor = .red
        pageArr.append(HWVoiceRoomGiftPageView())
        pageArr.append(HWVoiceRoomGiftPageView())
        pageArr.append(HWVoiceRoomGiftPageView())
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = titles
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#C2C2C2")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#FFFFFF")
        segmentedDataSource.titleNormalFont = UIFont.boldSystemFont(ofSize: 14)
        segmentedDataSource.titleSelectedFont = UIFont.boldSystemFont(ofSize: 14)
        segmentedDataSource.isItemSpacingAverageEnabled = false
        segmentedDataSource.itemSpacing = 30
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = .colorWithHexString(hexString: "#FFFFFF")
        indicator.lineStyle = .normal
        indicator.indicatorHeight = 2
        indicator.indicatorWidth = 20
        segmentedView.indicators = [indicator]

        addSubview(self.segmentedView)
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        segmentedView.delegate = self
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "#7D4DD2")
        addSubview(lineView)
    }
    
    private func initFrame() -> Void {
        segmentedView.snp.makeConstraints { make in
            make.left.top.right.equalTo(0)
            make.height.equalTo(35)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(segmentedView.snp.bottom).offset(5)
            make.height.equalTo(1)
        }
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.top.equalTo(lineView.snp.bottom).offset(0)
            make.bottom.equalTo(0)
        }
    }
}
extension HWVoiceRoomGiftSegmentView:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let pageView = HWVoiceRoomGiftPageView()
//        pageController.tagId = self.tagModelArr?[index].id ?? ""
        pageView.models = self.giftModels?[index].list
        pageView.didSelectGift = {
            mod in
            self.didSelectGift?(mod)
        }
        pageArr.remove(at: index)
        pageArr.insert(pageView, at: index)
        return pageView
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
//        for (idx,_) in self.giftModels!.enumerated() {
//
//        }
        for arr in self.giftModels?[index].list ?? [] {
            for mod in arr {
                mod.selected = false
            }
        }
        pageArr[index].models = self.giftModels?[index].list
    }
}
